package com.gomore.opple.module.lottery;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.gomore.opple.BuildConfig;
import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.model.EventActionFinish;
import com.gomore.opple.model.EventLottery;
import com.gomore.opple.model.LotteryModel;
import com.gomore.opple.module.BaseFragment;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.rest.employee.LoginResult;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.utils.JacksonUtils;

import butterknife.Bind;
import de.greenrobot.event.EventBus;


/**
 * Created by robot
 */
public class LotteryFragment extends BaseFragment implements LotteryContract.View {

    @Bind(R.id.web_view)
    WebView web_view;

    private LotteryContract.Presenter mPresenter;
    private String lotteryId;
    private String orderNumber;
    private String drawId;
    private String fragmentType;
    String WebUrl;

    public static LotteryFragment getInstance() {
        return new LotteryFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_lottery;
    }

    @Override
    protected void initalizeData() {
        if (getArguments() != null && getArguments().getString(IntentStart.LOTTERYID) != null) {
            lotteryId = getArguments().getString(IntentStart.LOTTERYID);
            orderNumber = getArguments().getString(IntentStart.ORDERID);
            drawId = getArguments().getString(IntentStart.DRAWID);
            fragmentType = getArguments().getString(IntentStart.FRAGMENT_TYPE);
        }
        if (drawId == null) {
            drawId = "";
        }
        WebUrl = BuildConfig.HOST.replace("app/", "") + "tOLotteryController.do?lotteryAward&lotteryId="
                + lotteryId + "&drawId="
                + drawId + "&orderNumber="
                + orderNumber;
        Log.e(BaseFragment.class.getName(), WebUrl);
    }

    @Override
    protected void initalizeViews() {
        mPresenter.prepareInitData();
        WebSettings websettings = web_view.getSettings();
        websettings.setSupportZoom(true);
        websettings.setBuiltInZoomControls(true);
        websettings.setJavaScriptEnabled(true);
        websettings.setJavaScriptCanOpenWindowsAutomatically(true);
        websettings.setAllowFileAccess(true);// 设置允许访问文件数据
        websettings.setDomStorageEnabled(true);
        websettings.setDatabaseEnabled(true);
        websettings.setUseWideViewPort(true);

        web_view.addJavascriptInterface(LotteryFragment.this, "Android");
        web_view.loadUrl(WebUrl);
        web_view.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showProgressDialog();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                hideProgressDialog();
            }
        });

        web_view.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

            }
        });
    }

    @Override
    public void setPresenter(LotteryContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    @Override
    public void showView() {
    }

    @JavascriptInterface
    public void setLotteryData(final String stringModel) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.e("12324", "13324235" + stringModel);
                LotteryModel lotteryModel = null;
                try {
                    if (!TextUtils.isEmpty(stringModel)) {
                        lotteryModel = JacksonUtils.fromJson(stringModel, LotteryModel.class);
                        EventLottery eventLottery = new EventLottery();
                        eventLottery.setType(fragmentType);
                        eventLottery.setLotteryModel(lotteryModel);
                        EventBus.getDefault().post(eventLottery);
                        showLotteryResult(lotteryModel);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onLeftIconClick() {
        EventActionFinish eventActionFinish = new EventActionFinish();
        eventActionFinish.setType(fragmentType);
        eventActionFinish.setIsFinish(true);
        EventBus.getDefault().post(eventActionFinish);
        super.onLeftIconClick();
    }

    //展示弹框信息
    private void showLotteryResult(LotteryModel lotteryModel) {
        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.result_dialog, null);
        TextView content = (TextView) layout.findViewById(R.id.content);
        TextView confirm = (TextView) layout.findViewById(R.id.confirm);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(layout);
        builder.setCancelable(false);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        content.setText("恭喜您,获得了" + lotteryModel.getDescription());
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //刷新我的抽奖界面
                EventActionFinish eventActionFinish = new EventActionFinish();
                eventActionFinish.setType(fragmentType);
                eventActionFinish.setIsFinish(false);
                EventBus.getDefault().post(eventActionFinish);
                getActivity().finish();
                alertDialog.dismiss();
            }
        });
    }
}
